package com.github.hongkaiwen.rxjava.play;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;

/**
 * 密级别：classify:p2#- User: hongkai Date: 15/08/2018 9:47 AM Description:
 */
public class W {

    public static void main(String... args){
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();

        final CountDownLatch latch = new CountDownLatch(1);
        final HttpGet request = new HttpGet("https://www.alipay.com/");

        System.out.println(" caller thread id is : " + Thread.currentThread().getId());

        httpclient.execute(request, new FutureCallback<HttpResponse>() {

            @Override
            public void completed(HttpResponse httpResponse) {
                latch.countDown();
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
                System.out.println(request.getRequestLine() + "->" + httpResponse.getStatusLine());
            }

            @Override
            public void failed(Exception e) {
                latch.countDown();
                System.out.println(request.getRequestLine() + "->" + e);
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
            }

            @Override
            public void cancelled() {

            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            httpclient.close();
        } catch (IOException ignore) {

        }
    }

}
